package com.neusmart.fs.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.neusmart.fs.F;
import com.neusmart.fs.R;
import com.neusmart.fs.constants.API;
import com.neusmart.fs.model.MParam;
import com.neusmart.fs.model.UserInfo;
import com.neusmart.fs.result.ResultGetProfileById;
import com.neusmart.fs.result.ResultIdentityAuthentication;
import com.neusmart.fs.view.dialog.OnHintListener;
import com.neusmart.fs.view.dialog.OnNewClickListener;

import java.util.ArrayList;
import java.util.List;

public class IdentityAuthenticationActivity extends DataLoadActivity implements View.OnClickListener,OnNewClickListener {
    private EditText username,identity;
    private Bundle bundle;
    private  int approveState;
    private  String cardNo,name,photo;
    private List<String> mList;
    private SharedPreferences setting;
    private  int sex;
    @Override
    protected int getLayoutResID() {
        return R.layout.activity_identity_authentication;
    }

    @Override
    protected void init() {
        loadExtraData();
        initView();
        setListener();
    }
    private void loadExtraData() {
        bundle = getIntent().getExtras();
        if (bundle == null)
            return;
    }

    private void initView() {
        setting = getSharedPreferences("test", 0);
        mList=new ArrayList<>();//身份证件url的集合
        username= (EditText) findViewById(R.id.username_edit);
        identity= (EditText) findViewById(R.id.identity_edit);
        username.setText(bundle.getString("userName"));
    }
    private void setListener() {
        int[] resIds = {R.id.my_data_btn_back,R.id.commit_button,R.id.shang_chuan};
        for (int id : resIds)
            findViewById(id).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_data_btn_back:
                onBackPressed();
                break;
            case R.id.shang_chuan:
                switchActivity(ChangeUserMassageLastActivity.class,bundle);//身份证学历
                break;
            case R.id.commit_button:
                if (username.getText().length()<=0){
                    showToast("请输入姓名");
                    return;
                }
                if (identity.getText().length()<=0){
                    showToast("请输入有效身份证号");
                    return;
                }else {
                    //showNewAlertDialog(getString(R.string.alertdialog_usermessage_change1),getString(R.string.alertdialog_change),getString(R.string.alertdialog_confirm),this);
                    if (F.mUserInfo.getApproveState()!=3){//认证过
                        showHintDialog("您已经使用了一次全国身份认证系统操作", new OnHintListener() {
                            @Override
                            public void onHint() {
                                String cardNo1 = setting.getString("cardNo", "");
                                bundle.putInt("approveState",approveState);
                                bundle.putString("cardNo", cardNo1);
                                switchActivity(ChangeUserMassageLastActivity.class,bundle);//身份证学历
                            }
                        });
                    }else {
                        showNewAlertDialog(getString(R.string.alertdialog_usermessage_change1),getString(R.string.alertdialog_change),getString(R.string.alertdialog_confirm),this);
                    }
                }
                break;
        }

    }
    @Override
    protected void initParams(MParam param) {
        switch (param.getApi()) {
            case GET_IDENTITY:
                param.addParam("cardNo", identity.getText().toString());
                param.addParam("realName", username.getText().toString());
                break;
            case UPDATE_PROFILE:
                param.addParam("name", username.getText().toString());
                param.addParam("identity_photo", mList);//身份
                param.addParam("identity_no", cardNo);
                param.addParam("sex", sex);
                param.addParam("needUpdateStatus",false);//1
                break;
            case GET_PROFILE_BY_ID:
                param.addParam("userid", F.mUserInfo.getUserId());
                break;
            default:
                break;
        }
    }
    @Override
    protected void disposeResult(API api, String response) {
        Log.i("kkk", "disposeResult: ==="+response);
        if (response == null)
            return;
        switch (api) {
            case GET_IDENTITY:
                ResultIdentityAuthentication resultIdentityAuthentication = fromJson(response, ResultIdentityAuthentication.class);
                if (!resultIdentityAuthentication.isSuccess())
                    return;
                name = resultIdentityAuthentication.getData().getName();
                approveState = resultIdentityAuthentication.getData().getApproveState();
                cardNo = resultIdentityAuthentication.getData().getCardNo();
                sex = resultIdentityAuthentication.getData().getSex();
                setting.edit().putString("cardNo",cardNo).commit();
                photo = resultIdentityAuthentication.getData().getPhoto();
                if (approveState==1){//验证不通过
                    loadData(API.GET_PROFILE_BY_ID, false);
                    showHintDialog("认证不成功，请直接上传身份证照片认证", new OnHintListener() {
                        @Override
                        public void onHint() {
                            bundle.putInt("approveState",approveState);
                            bundle.putString("cardNo",cardNo);
                            switchActivity(ChangeUserMassageLastActivity.class,bundle);//身份证学历
                        }
                    });
                }else if (approveState==2){//通过验证
                    showToast("认证成功");
                     //提交数据
                    mList.add(photo);
                    loadData(API.UPDATE_PROFILE, true);
                }
                break;
            case UPDATE_PROFILE:
                loadData(API.GET_PROFILE_BY_ID, false);
                break;
            case GET_PROFILE_BY_ID:
                ResultGetProfileById resultGetProfileById = fromJson(response, ResultGetProfileById.class);
                if (!resultGetProfileById.isSuccess()) {
                    showToast(resultGetProfileById.getFriendlyMessage());
                    return;
                }
                UserInfo mUserInfo =  resultGetProfileById.getData();
                mUserInfo.setToken(F.mUserInfo.getToken());
                F.mUserInfo = mUserInfo;
                F.login(mUserInfo);
                bundle.putInt("approveState",approveState);
                bundle.putString("cardNo",cardNo);
                if (approveState==2){
                    switchActivity(ChangeUserMassageLastActivity.class,bundle);//身份证学历页面
                }
                break;
            default:
                break;
        }
    }
    @Override
    public void onLeftClick() {

    }
    @Override
    public void onRightClick() {
        loadData(API.GET_IDENTITY,true);
    }
}